// -------------------------------------------------------------
// Mesh Cloth Maker STOOL - v0.8
// Made By: Suicidal.Banana
// -------------------------------------------------------------
// Disclaimer: You are free to use bits of code from my stool, 
// as long as you GIVE ME CREDIT, Suicidal.Banana
// -------------------------------------------------------------

	TOOL.Category		= "Physics"
	TOOL.Name		= "#Mesh Cloth"
	TOOL.Command		= nil
	TOOL.ConfigName		= ""

	TOOL.ClientConVar[ "width" ] = "50"
	TOOL.ClientConVar[ "height" ] = "50"
	TOOL.ClientConVar[ "spheredistance" ] = "20"
	TOOL.ClientConVar[ "spawnheight" ] = "35"
	
	TOOL.ClientConVar[ "meshheight" ] = "0"
	TOOL.ClientConVar[ "meshdisplay" ] = "0"
	TOOL.ClientConVar[ "ropedisplay" ] = "1"
	TOOL.ClientConVar[ "meshmaterial" ] = "mesh_cloth/greencamo"


// Add Default Language translation (saves adding it to the txt files)
if ( CLIENT ) then

	language.Add( "Tool_mesh_cloth_maker_name", "Mesh Cloth Maker" )
	language.Add( "Tool_mesh_cloth_maker_desc", "Make Mesh Cloth" )
	language.Add( "Tool_mesh_cloth_maker_0", "..." )
	language.Add( "Undone_Mesh Cloth", "Undone Mesh Cloth" )
	
end

cleanup.Register( "sents" )

function TOOL:Reload()
	if SERVER then 
		local ply = self:GetOwner()
		local suspnpcname = "MeshClothSphere_" .. ply:SteamID() .. "*"
		local checkifthere = ents.FindByName( suspnpcname )
			for K, V in pairs(checkifthere) do 
				local physen = V:GetPhysicsObject()
				physen:EnableMotion(true)
				physen:Wake()
				V:SetColor( 0, 0, 0, 1 )
			end
	end  
end

function TOOL:LeftClick( trace )

if SERVER then
		local ply = self:GetOwner()
		if ( !ply:CheckLimit( "sents" ) ) then return false end
		local spheredist = ply:GetInfo( "mesh_cloth_maker_spheredistance" )
		local spherecountx = math.Round(ply:GetInfo( "mesh_cloth_maker_width" ) / spheredist)
		local spherecounty = math.Round(ply:GetInfo( "mesh_cloth_maker_height" ) / spheredist)
		local meshmaterial = ply:GetInfo( "mesh_cloth_maker_meshmaterial" )
		-- disabled for now
		-- local meshheight = ply:GetInfo( "mesh_cloth_maker_meshheight" )
		local meshheight = 0
		local spawnheight = ply:GetInfo( "mesh_cloth_maker_spawnheight" )
		local meshdisplay = ply:GetInfo( "mesh_cloth_maker_meshdisplay" )
		local ropedisplay = ply:GetInfo( "mesh_cloth_maker_ropedisplay" )
		local clothcount = math.Rand(0,10000)
		
		
		undo.Create("Mesh Cloth")
		
	
		local meshclothconfig = ents.Create("MeshCloth_MeshConfig")
					meshclothconfig:SetPos(trace.HitPos + trace.HitNormal * 10 + Vector(0,0,spawnheight+15))
					meshclothconfig:SetName( "MeshCloth_MeshConfig_" .. ply:SteamID() .. "_count" .. clothcount)
					meshclothconfig:SetKeyValue("theplyid", ply:SteamID() )
					meshclothconfig:SetKeyValue("theclothcount", clothcount )
					meshclothconfig:SetKeyValue("meshmaterial", meshmaterial )
					meshclothconfig:SetKeyValue("meshheight", meshheight )
					meshclothconfig:SetKeyValue("meshdisplay", meshdisplay )
					meshclothconfig:SetKeyValue("thelastx", spherecountx )
					meshclothconfig:SetKeyValue("thelasty", spherecounty )
				meshclothconfig:Spawn()
				
	
	for countx=0, spherecountx, 1 do
		for county=0, spherecounty, 1 do
		
			local vectorx = spheredist*countx
			local vectory = spheredist*county
			local centerAx = spherecountx / 2
			local centerAy = spherecounty / 2
			local centerx = centerAx * spheredist
			local centery = centerAy * spheredist
			local plyid = ply:SteamID()
			
				local meshclothsphere = ents.Create("MeshCloth_Sphere")
					meshclothsphere:SetPos(trace.HitPos + trace.HitNormal * 10  + Vector(-1*centerx,-1*centery,0) + Vector(vectorx,vectory,spawnheight))
					meshclothsphere:SetName( "MeshClothSphere_" .. ply:SteamID() .. "_count" .. clothcount .. "_x" .. countx .. "_y" .. county )
					meshclothsphere:SetKeyValue("theplyid", ply:SteamID() )
					meshclothsphere:SetKeyValue("countx", countx )
					meshclothsphere:SetKeyValue("county", county )
					meshclothsphere:SetKeyValue("theclothcount", clothcount )
					meshclothsphere:SetKeyValue("ropeleng", spheredist )
					meshclothsphere:SetKeyValue("showrope", ropedisplay )
					meshclothsphere:SetKeyValue("meshdisplay", meshdisplay )
				meshclothsphere:Spawn()

				ply:AddCount( "sents", meshclothsphere )
				ply:AddCleanup( "sents", meshclothsphere )
				undo.AddEntity( meshclothsphere )

		end
	end
	if tonumber(meshdisplay) > 0 then
		meshclothconfig:createthemesh()
	else
		meshclothconfig:Remove()
	end
	undo.SetPlayer( ply )
	undo.Finish()		
end

end

function TOOL:RightClick( trace )
	
end

function TOOL.BuildCPanel( CPanel )

CPanel:AddControl( "Header", { Text = "General settings", Description	= "Here you can set up quaity, what to show, width, height and spawnheight" }  )	

	local Qual_params = {Label = "Quality:", MenuButton = "0", Options = {}}
		Qual_params.Options["Low"] = { mesh_cloth_maker_spheredistance = "20" }
		Qual_params.Options["Medium"] = { mesh_cloth_maker_spheredistance = "15" }
		Qual_params.Options["High"] = { mesh_cloth_maker_spheredistance = "10" }
	CPanel:AddControl( "ComboBox", Qual_params )	

	local Show_params = {Label = "Show:", MenuButton = "0", Options = {}}
		Show_params.Options["Net"] = { mesh_cloth_maker_meshdisplay = "0", mesh_cloth_maker_ropedisplay = "1" }
		Show_params.Options["Cloth"] = { mesh_cloth_maker_meshdisplay = "1", mesh_cloth_maker_ropedisplay = "0" }
		Show_params.Options["Net and Cloth"] = { mesh_cloth_maker_meshdisplay = "1", mesh_cloth_maker_ropedisplay = "1" }
	CPanel:AddControl( "ComboBox", Show_params )	

	CPanel:AddControl( "Slider",  { Label	= "#Width:",	Type	= "Float", Min = 50, Max = 150, Command = "mesh_cloth_maker_width" }	 )
					
	CPanel:AddControl( "Slider",  { Label	= "#Height:", Type	= "Float", Min = 50, Max = 150, Command = "mesh_cloth_maker_height" }	 )
	
	CPanel:AddControl( "Slider",  { Label	= "#Spawn Height:", Type	= "Float", Min = 5, Max = 100, Command = "mesh_cloth_maker_spawnheight" }	 )


CPanel:AddControl( "Header", { Text = "Cloth settings", Description	= "Here you can set up the height and material of the cloth" }  )		
	
	-- disabled for now				
	-- CPanel:AddControl( "Slider",  { Label	= "#Height", Type = "Float", Min = 0.5, Max = 5, Command = "mesh_cloth_maker_meshheight" }	 )
					
	local fabric_params = {Label = "Material:", Height = "45", Width = "45", Rows = "1", Stretch = "1", Options = {}}
	
	
	fabric_params.Options["Black jeans"] = { Material = "mesh_cloth/blackjeans",
				    mesh_cloth_maker_meshmaterial = "mesh_cloth/blackjeans"
				    }
	fabric_params.Options["Blue jeans"] = { Material = "mesh_cloth/bluejeans",
				    mesh_cloth_maker_meshmaterial = "mesh_cloth/bluejeans"
				    }
	fabric_params.Options["Desert Camoflage"] = { Material = "mesh_cloth/desertcamo",
				    mesh_cloth_maker_meshmaterial = "mesh_cloth/desertcamo"
				    }
	fabric_params.Options["Green Camoflage"] = { Material = "mesh_cloth/greencamo",
				    mesh_cloth_maker_meshmaterial = "mesh_cloth/greencamo"
				    }

	CPanel:AddControl( "MaterialGallery", fabric_params )
	

	
end
